clear
set more off, perm
cd /Users/zachbrown/Projects/PriceTransparency/Data/

use clean/radiology_visit_3.dta, clear
desc serv_prov_zip member_zip 
bys serv_prov_cw_key2: egen prov_id = mode(serv_ind_cw)
drop if prov_id==.

// Mode zip
rename serv_prov_zip tmp_serv_prov_zip
bys prov_id year: egen serv_prov_zip=mode(tmp_serv_prov_zip), missing
drop tmp_serv_prov_zip

// Merge on county
rename member_zip zip
merge m:1 zip using dist/zcta_county_rel_10.dta
rename zip member_zip
rename county member_county
drop _merge
replace member_county=-1 if member_county==.

gen n=1
collapse (sum) n (mean) amt_billed amt_billed_radio amt_allowed amt_allowed_radio amt_member_oop amt_oop_radio ///
	, by(prov_id year member_county proc_id_radio payernm_short)
drop if n<10
collapse (p10) amt_oop_10=amt_member_oop amt_allowed_10=amt_allowed amt_billed_10=amt_billed ///
	amt_oopr_10=amt_oop_radio amt_allowedr_10=amt_allowed_radio amt_billedr_10=amt_billed_radio ///
	, by(member_county proc_id_radio year payernm_short)
save clean/price_ptiles.dta, replace

